package com.hns.tool.pub;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 哈希化工具，主要对对象数组或者list进行哈希化操作，生成hashMap或者set
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
//TODO
public class Hashlize {
	/**
	 * 构造
	 * @email:zhuangcaijin@126.com
	 */
	public Hashlize() {
		super();
	}

	/**
	 * 从指定对象数组中遍历，根据数组中每个元素对象对应的key分类，并生成对应list，并将这些list和对应的key作为key-value放入map中
	 * @param objs 对象数组
	 * @param iHashKey hashkey适配器对象
	 * @return 生成的map
	 * @author:Fisher
	 * @email:zhuangcaijin@126.com
	 */
	//TODO
	@SuppressWarnings("unchecked")
	public static HashMap hashlizeObjects(Object[] objs, IHashKey iHashKey) {
		if (objs == null || objs.length == 0)
			return null;

		if (iHashKey == null)
			throw new RuntimeException("iHashKey cann't be null");

		HashMap result = new HashMap();

		for (int i = 0; i < objs.length; i++) {
			String key = iHashKey.getKey(objs[i]);
			ArrayList al = (ArrayList) result.get(key);
			if (al == null) {
				al = new ArrayList();
				result.put(key, al);
			}
			al.add(objs[i]);
		}

		return result;
	}

	/**
	 * 从指定对象list中遍历，根据list中每个对象对应的key分类，并生成对应list，并将这些list和对应的key作为key-value放入map中
	 * @param objs 对象list
	 * @param iHashKey hashkey适配器对象
	 * @return 生成的map
	 * @author:Fisher
	 * @email:zhuangcaijin@126.com
	 */
	//TODO
	@SuppressWarnings("unchecked")
	public static HashMap hashlizeObjects(List objs, IHashKey iHashKey) {
		if (objs == null || objs.size() == 0)
			return null;

		if (iHashKey == null)
			throw new RuntimeException("iHashKey cann't be null");

		HashMap result = new HashMap();

		for (int i = 0; i < objs.size(); i++) {
			String key = iHashKey.getKey(objs.get(i));
			ArrayList al = (ArrayList) result.get(key);
			if (al == null) {
				al = new ArrayList();
				result.put(key, al);
			}
			al.add(objs.get(i));
		}

		return result;
	}

	/**
	 * 将对象数组遍历，找出每个元素对象对应的key，并组成逐个key-value放入map
	 * @param objs 对象数组
	 * @param iHashKey hashkey适配器对象
	 * @return 生成的map
	 * @author:Fisher
	 * @email:zhuangcaijin@126.com
	 */
	//TODO
	public static HashMap hashlizeMap(Object[] objs, IHashKey iHashKey){
		if (objs == null || objs.length == 0)
			return null;

		if (iHashKey == null)
			throw new RuntimeException("iHashKey cann't be null");

		HashMap result = new HashMap();

		for (int i = 0; i < objs.length; i++) {
			String key = iHashKey.getKey(objs[i]);
			result.put(key, objs[i]);
		}

		return result;
	}

	/**
	 * 将对象list遍历，找出每个对象对应的key，并组成逐个key-value放入map
	 * @param objs 对象list
	 * @param iHashKey hashkey适配器对象
	 * @return 生成的map
	 * @author:Fisher
	 * @email:zhuangcaijin@126.com
	 */
	//TODO
	public static HashMap hashlizeMap(List objs, IHashKey iHashKey) {
		if (objs == null || objs.size() == 0)
			return null;

		if (iHashKey == null)
			throw new RuntimeException("iHashKey cann't be null");
		HashMap result = new HashMap();

		for (int i = 0; i < objs.size(); i++) {
			String key = iHashKey.getKey(objs.get(i));
			result.put(key, objs.get(i));
		}

		return result;
	}

	/**
	 * 遍历对象数组，根据数组元素找出key，并根据key找出map中对应值和数组元素进行组合操作，重新放入key对应value中，更新已有map
	 * @param objs 对象数组
	 * @param iHashKey hashkey适配器对象
	 * @param iCombine 组合对象
	 * @return 生成的map
	 * @author:Fisher
	 * @email:zhuangcaijin@126.com
	 */
	//TODO
	public static HashMap hashlizeObjects(Object[] objs, IHashKey iHashKey, ICombine iCombine) {
		if (objs == null || objs.length == 0)
			return null;

		if (iHashKey == null)
			throw new RuntimeException("iHashKey cann't be null");
		HashMap result = new HashMap();

		for (int i = 0; i < objs.length; i++) {
			String key = iHashKey.getKey(objs[i]);
			Object o = result.get(key);
			result.put(key, iCombine.combine(o, objs[i]));
		}
		return result;
	}
	
	/**
	 * 遍历对象list，根据数组list找出key，并根据key找出map中对应值和数组元素进行组合操作，重新放入key对应value中，更新已有map
	 * @param objs 对象list
	 * @param iHashKey hashkey适配器对象
	 * @param iCombine 组合对象
	 * @return 生成的map
	 * @author:Fisher
	 * @email:zhuangcaijin@126.com
	 */
	public static HashMap hashlizeObjects(List objs, IHashKey iHashKey, ICombine iCombine) {
		if (objs == null || objs.size() == 0)
			return null;

		if (iHashKey == null)
			throw new RuntimeException("iHashKey cann't be null");
		HashMap result = new HashMap();

		int size = objs.size();
		for (int i = 0; i < size; i++) {
			String key = iHashKey.getKey(objs.get(i));
			Object o = result.get(key);
			result.put(key, iCombine.combine(o, objs.get(i)));
		}
		return result;
	}
	
	/**
	 * 将对象list遍历，找出每个对象对应的key，并将key放入set中
	 * @param objs 对象list
	 * @param iHashKey hashkey适配器对象
	 * @return 生成的set
	 * @author:Fisher
	 * @email:zhuangcaijin@126.com
	 */
	public static Set<String> hashlizeSet(List objs, IHashKey iHashKey){
		if (PubUtil.isEmpty(objs))
			return null;
		
		if (iHashKey == null)
			throw new RuntimeException("iHashKey cann't be null");

		Set<String> result = new HashSet();

		for (int i = 0; i < objs.size(); i++) {
			String key = iHashKey.getKey(objs.get(i));
			result.add(key);
		}
		return result;
	}
	
}
